package org.victoriest.server.handler;


import org.apache.log4j.Logger;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.victoriest.server.dto.LoginModel;
import org.victoriest.server.dto.MessageModel;
import org.victoriest.server.event.VictoriestEvent;
import org.victoriest.server.protocol.Protocol;
import org.victoriest.server.util.MessageSender;

public class ClientHandler extends SimpleChannelHandler {

	protected Logger LOGGER = Logger.getLogger(getClass());

	@Override
	public void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		LOGGER.info("bindRequested! id:" + e.getChannel().getId());
	}

	@Override
	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
		LOGGER.info("A channel has connected! id:" + e.getChannel().getId());
		LoginModel model = new LoginModel();
		model.setChannelId(e.getChannel().getId());
		model.setName("user_" + model.getChannelId());
		MessageSender.write(e.getChannel(), Protocol.TYPE_LOGIN_REQ, model);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
		VictoriestEvent event = (VictoriestEvent) e.getMessage();
		// TODO
		switch (event.getType()) {
		case TYPE_LOGIN_RES:
			LOGGER.info("TYPE_LOGIN_RES result : " + event.getSource());
			break;
		case TYPE_ENTER_HALL_RES:
			LOGGER.info("TYPE_ENTER_HALL_RES result : " + event.getSource());
			break;
		case TYPE_CREATE_ROOM_RES:
			LOGGER.info("TYPE_CREATE_ROOM_RES result : " + event.getSource());
			break;
		case TYPE_ENTER_ROOM_RES:
			LOGGER.info("TYPE_ENTER_ROOM_RES result : " + event.getSource());
			break;
		case TYPE_FEACH_ROOM_RES:
			LOGGER.info("TYPE_FEACH_ROOM_RES");
			String[] groups = (String[]) event.getSource();
			for(String str : groups){
				System.out.println(str);
			}
			break;
		case TYPE_CHAT_RES:
			LOGGER.info("TYPE_CHAT_RES");
			MessageModel msg = (MessageModel) event.getSource();
			String str = String.format("[%s]%s(%s) : %s", msg.getType(),
					msg.getSenderName(), msg.getSenderChannelId(),
					msg.getContent());
			LOGGER.info(str);
			break;
		case TYPE_QUIT_ROOM_RES:
			LOGGER.info("TYPE_QUIT_ROOM_RES result : " + event.getSource());
			break;
		case TYPE_QUIT_HALL_RES:
			LOGGER.info("TYPE_QUIT_HALL_RES result : " + event.getSource());
			break;
		default:
		}
	}

	@Override
	public void channelDisconnected(ChannelHandlerContext ctx,
			ChannelStateEvent e) throws Exception {
		LOGGER.info("channelDisconnected! id:" + e.getChannel().getId());
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
		LOGGER.warn("Unexpected exception ", e.getCause());
		e.getChannel().close();
	}

}